package xdpm.nhom16.BUS;

import java.util.ArrayList;
import java.util.Collections;

import xdpm.nhom16.DAO.AbstractDAO;
import xdpm.nhom16.DAO.DichVuDAO;
import xdpm.nhom16.DAO.DuLieuDAO;
import xdpm.nhom16.DAO.DuongDAO;
import xdpm.nhom16.DAO.ISearchAble;
import xdpm.nhom16.DAO.TuKhoaDiaDiemDAO;
import xdpm.nhom16.DAO.TuKhoaDichVuDAO;
import xdpm.nhom16.DAO.TuKhoaDuongDAO;
import xdpm.nhom16.DTO.DuLieuSearchDTO;
import xdpm.nhom16.DTO.TuKhoaDichVuDTO;
import xdpm.nhom16.DTO.TuKhoaDuongDTO;
import xdpm.nhom16.GUI.PhanTu;
import xdpm.nhom16.helper.GiaCaoNhatComparator;
import xdpm.nhom16.helper.GiaThapComparator;
import xdpm.nhom16.helper.TenDiaDiemComparator;
import xdpm.nhom16.helper.TenDichVuComparator;
import xdpm.nhom16.helper.Utilities;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;

public class SearchEngine implements ISearchAble<DuLieuSearchDTO> {
	private Context context;
	private TuKhoaDichVuDAO tuKhoaDichVuDAO = null;
	private TuKhoaDuongDAO tuKhoaDuongDAO = null;
	private DuLieuDAO duLieuDAO = null;
	private SQLiteDatabase db;
	public SearchEngine(Context context) {
		this.context = context;
		db = DataProvider.getInstance(this.context).getDb();
		new TuKhoaDiaDiemDAO(db);
		this.duLieuDAO = new DuLieuDAO(db);
		this.tuKhoaDichVuDAO = new TuKhoaDichVuDAO(db);
		this.tuKhoaDuongDAO = new TuKhoaDuongDAO(db);
	}

	public ArrayList<PhanTu> reference(ArrayList<DuLieuSearchDTO> dulieuSearchs,
			int indexStart, int indexEnd) {
		ArrayList<PhanTu> lstPhanTu = new ArrayList<PhanTu>();
		Bitmap icon[] = Utilities.getDichVuIcon(context);
		int n = dulieuSearchs.size();
		if (n < 1)
			return lstPhanTu;
		int validStart = indexStart > -1 ? indexStart : 0;
		int validEnd = indexEnd < n ? indexEnd + 1 : n;
		if (indexStart > indexEnd) {
			return lstPhanTu;
		}

		for (int i = validStart; i < validEnd; i++) {
			PhanTu phanTu = new PhanTu(
					icon[dulieuSearchs.get(i).getMaDichVu() - 1],
					dulieuSearchs.get(i).getTenDiaDiem(),
					dulieuSearchs.get(i).getDiaChi());
			lstPhanTu.add(phanTu);
		}
		return lstPhanTu;
	}
	public ArrayList<DuLieuSearchDTO> search(String input) {
		ArrayList<DuLieuSearchDTO> lstSearch = new ArrayList<DuLieuSearchDTO>();

		if (input == null || input.trim().length() == 0)
			return lstSearch;

		final String inputFormatted = Utilities.formatInputData(input);

		lstSearch = searchTuKhoaDichVu(inputFormatted);
		return lstSearch;
	}

	private ArrayList<DuLieuSearchDTO> searchTuKhoaDichVu(final String input) {
		ArrayList<DuLieuSearchDTO> lstSearch = new ArrayList<DuLieuSearchDTO>();
		ArrayList<TuKhoaDichVuDTO> tuKhoas = null;
		tuKhoas = tuKhoaDichVuDAO.search(input);
		if (tuKhoas.size() > 0) {
			for (TuKhoaDichVuDTO tuKhoa : tuKhoas) {
				String inputTemp = Utilities.clear(input,
						tuKhoa.get_tuKhoaDichVu());

				if (inputTemp.length() > 0) {
					lstSearch.addAll(searchTuKhoaDuong(inputTemp, tuKhoa.get_maDichVu()));
				} else {
					lstSearch.addAll(duLieuDAO.searchForSort(tuKhoa.get_maDichVu(), 0));
				}
			}
		}
		lstSearch.addAll(searchTuKhoaDuong(input, AbstractDAO.DEFAULT_NUMBER));
		return lstSearch;
	}

	private ArrayList<DuLieuSearchDTO> searchTuKhoaDuong(String input, int maDichVu) {
		ArrayList<DuLieuSearchDTO> lstSearch = new ArrayList<DuLieuSearchDTO>();
		ArrayList<TuKhoaDuongDTO> tuKhoas = null;
		tuKhoas = tuKhoaDuongDAO.search(input);
		if (tuKhoas.size() > 0) {
			for (TuKhoaDuongDTO tuKhoa : tuKhoas) {
				String inputTemp = Utilities.clear(input,
						tuKhoa.getTuKhoaDuong());

				if (inputTemp.length() == 0) {
					lstSearch.addAll(duLieuDAO.searchForSort(maDichVu, tuKhoa.getMaDuong()));
				}
			}
		}
		return lstSearch;
	}
	public ArrayList<DuLieuSearchDTO> sortByTenDiaDiem(ArrayList<DuLieuSearchDTO> inList, int order)	{
		if (order == 0)	{/*Ascending*/
			Collections.sort(inList, new TenDiaDiemComparator());
		}
		else	{/*Desending*/
			Collections.sort(inList, Collections.reverseOrder(new TenDiaDiemComparator()));
		}
		return inList;
	}
	public ArrayList<DuLieuSearchDTO> sortByTenDichVu(ArrayList<DuLieuSearchDTO> inList, int order)	{
		if (order == 0)	{/*Ascending*/
			Collections.sort(inList, new TenDichVuComparator());
		}
		else	{/*Desending*/
			Collections.sort(inList, Collections.reverseOrder(new TenDichVuComparator()));
		}
		return inList;
	}
	public ArrayList<DuLieuSearchDTO> sortByGiaTien(ArrayList<DuLieuSearchDTO> inList, int order)	{
		if (order == 0)	{/*Cao nhất -> Giảm*/
			Collections.sort(inList, Collections.reverseOrder(new GiaCaoNhatComparator()));
		}
		else	{/*Thấp nhất -> Tăng*/
			Collections.sort(inList, new GiaThapComparator());
		}
		return inList;
	}
	public ArrayList<String> getTenDichVu (){
		ArrayList<String> listDichVu = new ArrayList<String>();
		DichVuDAO dichVuDAO = new DichVuDAO(db);
		listDichVu = dichVuDAO.getTenDichVu();
		return listDichVu;	
	}
	
	public ArrayList<String> getTenDuong (){
		ArrayList<String> listDuong = new ArrayList<String>();
		DuongDAO duongDAO = new DuongDAO(db);
		listDuong = duongDAO.getTenDuong();
		return listDuong;	
	}
}
